package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdComm;
import com.powernode.model.Result;
import com.powernode.service.ProdCommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/2/24 11:30
 */
@RestController
@Slf4j
@RequestMapping("prod/prodComm")
@Api(tags = "商品评论管理接口")
public class ProdCommController {


    @Autowired
    private ProdCommService prodCommService;

    /**
     * 分页查询商品评论
     * @return
     */
    @ApiOperation("分页查询商品评论")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page> loadProdCommPage(@RequestParam(value = "current") Long current,
                                           @RequestParam(value = "size") Long size,
                                           @RequestParam(value = "prodName", required = false) String prodName,
                                           @RequestParam(value = "status", required = false) Integer status
                                           ){
        Page page = new Page<ProdComm>(current,size);

        page = prodCommService.page(page,
                new LambdaQueryWrapper<ProdComm>()
                        .like(StringUtils.hasText(prodName), ProdComm::getProdName, prodName)
                        .eq(status != null, ProdComm::getStatus, status)
                        .orderByDesc(ProdComm::getCreateTime)
        );

        return Result.success(page);
    }

    /**
     * 根据id查询商品评论
     * @param prodCommId
     * @return
     */
    @ApiOperation("根据id查询商品评论")
    @GetMapping("{prodCommId}")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> getProdCommInfo(@PathVariable(value = "prodCommId") Long prodCommId){
        ProdComm byId = prodCommService.getById(prodCommId);
        return Result.success(byId);
    }

    /**
     * 修改商品评论状态
     * @return
     */
    @ApiOperation("修改商品评论状态")
    @PutMapping()
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<String> updateProdCommStatus(@RequestBody ProdComm prodComm){

        boolean b = prodCommService.modifyProdCommById(prodComm);
        //boolean b = prodCommService.updateById(prodComm);
        return Result.handle(b);
    }

}
